package rgbos.forum.search;

import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Vector;

import rgbos.forum.domain.ForumCtrlPost;
import rgbos.forum.domain.Post;
import rgbos.forum.persistance.Persistance;

public class PrimitiveSearchEngine implements AbstractSearchEngine {

	private SearchIndex searchIndex_;
	private SearchAgent searchAgent_;
	private Persistance persistance_;

	public PrimitiveSearchEngine() {
		searchIndex_ = new SearchIndex();
		searchAgent_ = new SearchAgent(searchIndex_);
		
		persistance_ = new Persistance("SQL");
		persistance_.connect();		
		Thread searchAgent = new Thread(searchAgent_);
		searchAgent.start();
	}

	public Vector<PostRate> searchByContent(String searchQuery) {
		boolean tSearchByPhrase = false;
		if (searchQuery.length() > 0 && searchQuery.charAt(0) == '"' &
				searchQuery.charAt(searchQuery.length()-1) == '"') {
			tSearchByPhrase = true;
			searchQuery = searchQuery.substring(1, searchQuery.length()-2);
		}

		String[] tWords = searchQuery.split(" ");
		for (int i = 0; i < tWords.length; i++) {
			tWords[i] = tWords[i].toLowerCase();
		}
		Vector<PostRate> tPosts = new Vector<PostRate>();

		if (tWords.length == 0)
			return tPosts;

		Integer tWordId = searchIndex_.getWordToWordId_().get(tWords[0]);

		if (tWordId == null)
			return tPosts;

		HashSet<Integer> tPostsId = searchIndex_.getWordIdToPostId_().get(tWordId);

		for (Integer postId : tPostsId) {
			if (tSearchByPhrase) {
				Post tPost = persistance_.getPostById(postId);				
				CharSequence tCharSeq = searchQuery.subSequence(0, searchQuery.length()-1);
				if (tPost.getBody_().contains(tCharSeq))
					tPosts.add(new PostRate(tPost, 1));
			}
			else {
				boolean tInAll = true;
				for (int i = 1; i < tWords.length & tInAll; i++) {
					tWordId = searchIndex_.getWordToWordId_().get(tWords[i]);
					if (tWordId != null) {
						HashSet<Integer> tHs = searchIndex_.getWordIdToPostId_().get(tWordId);
						tInAll = tHs.contains(postId);
					}
				}
				if (tInAll) {
					Post tPost = persistance_.getPostById(postId);					
					tPosts.add(new PostRate(tPost, 1));
				}
			}
		}

		return tPosts;
	}

	public Vector<PostRate> searchByAuthor(String name) {
		Vector<Post> tPosts = persistance_.getPosts();
		if (tPosts == null)
			tPosts = new Vector<Post>();

		Vector<PostRate> tAns = new Vector<PostRate>();

		for (Post post : tPosts) {
			if (post.getAuthorName().toLowerCase().contentEquals(name.toLowerCase())) {				
				tAns.add(new PostRate(post, 1));
			}
		}
		return tAns;
	}

	public Vector<PostRate> searchByDate(String date) {
		Vector<Post> tPosts = persistance_.getPosts();
		if (tPosts == null)
			tPosts = new Vector<Post>();

		Vector<PostRate> tAns = new Vector<PostRate>();
		SimpleDateFormat tDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		for (Post post : tPosts) {
			String tPostDate = tDateFormat.format(post.getCreationDate_());
			if (date.contentEquals(tPostDate))
				tAns.add(new PostRate(post, 1));
		}
		return tAns;
	}

	public void indexPost(Post message) {
		searchAgent_.indexPost(message);
	}

	@Override
	public void deletePost(Post message) {
		// this search doesn't support this function
	}

	@Override
	public void modifyPost(Post oldMessage, Post newMessage) {
		// this search doesn't support this function
	}

	@Override
	public Vector<PostRate> searchByDate(String from, String to) {
		return new Vector<PostRate>();
	}
}
